## server.tcl

proc puti {str} {
    .tx.text insert end "$str\n"
 }
 
frame .tx
text .tx.text -yscrollcommand ".tx.scroll set"
scrollbar .tx.scroll -command ".tx.text yview"
pack .tx.scroll -side right -fill y
pack .tx.text -side left
pack .tx -side top

source wish.tcl

# source vsg.tcl
# source drag_in_box1.tcl
# wm withdraw "."

proc read_sock {wsock} {
  global  eventLoop
  set l [gets $wsock]
  if {[eof $wsock]} {
    close $wsock             ;# close the socket client connection
    set eventLoop "done"     ;# terminate the vwait (eventloop)
  } else {
    ## uncomment for debugging
    ## puti "read $l"
    eval $l
  }
}


# sleep for val milliseconds

proc sleep { val } {
    set x 0
    after $val {set x 1}
    vwait x
}

proc sendSocket  { cmd } {
    global sock
    puts $sock $cmd 
}

proc sendCheckButton  { x v } {
    global sock
    global vv
    puts $sock "$x $vv($v)"
}

proc sendRadioButton  { x v } {
    global sock
    global vv
    puts $sock "$x $vv($v)"
}

proc theSock {} {
    global sock
    return $sock
}

## this is a mess -- all other varients seem not to work :-(

proc sendCombo  { x } {
    global sock
    append z $x " getcurselection"
    set y [eval $z]
    puts $sock "$x $y"
}

proc getListSelection { x } {
    global sock
    puts $sock "$x [$x get active]"
}

proc readEntry { x } {
    global sock
    puts $sock "$x [$x get]"
}


puti "connect to port $argv"
puti "Erlang will terminate when the counter is zero"
puti "Or you can ^C erlang to seewhat happens"

## open the socket the indicated port on localhost

set sock [socket localhost $argv]

fileevent $sock readable [list read_sock $sock]

fconfigure $sock -buffering line


# reply

puts $sock "magic sync"

puti "waiting for data "

vwait eventLoop
puti "Client Finished ... I'll exit in 5 seconds"
sleep 5000
exit
